package cn.wsgfz.test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Picture;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import cn.wsgfz.test.DifficultEntity.DifficultItem;
import cn.wsgfz.testsvgapp.R;
import cn.wsgfz.widget.PictureDrawablePlus;

@SuppressLint("DefaultLocale")
public class DifficultHandler {
	private Context mContext;
	
	public DifficultHandler(Context ctx) {
		this.mContext = ctx;
	}
	
	public DifficultEntity getDifficultCpu(ImageView imageView) {
		DifficultEntity entity = new DifficultEntity();
		entity.title = DifficultDataUtils.use_cpu_name;
		entity.items = new CopyOnWriteArrayList<DifficultItem>();
		
		entity.items.addAll(getDifficultItems(R.drawable.a16, R.raw.icon01, imageView));
//		entity.items.addAll(getDifficultItems(R.drawable.b16, R.raw.icon02, imageView));
		return entity;
	}
	
	private List<DifficultItem> getDifficultItems(int dId, int sId, ImageView imageView) {
		long countTime = 0L;
		Drawable drwa = null;
		Resources res  = mContext.getResources();
		long biginTime = System.currentTimeMillis();
		List<DifficultItem> list = new CopyOnWriteArrayList<DifficultItem>();
		
		for(int i = 0; i < 1000; i ++) {
			drwa = res.getDrawable(dId);
//			imageView.setImageDrawable(drwa);
		}
		
		long sTime    = System.currentTimeMillis() - biginTime;
		countTime     = countTime + sTime;
		String sAbout =  "time:" + sTime + "ms/1000张";
		biginTime = System.currentTimeMillis();
		
		for(int i = 0; i < 1000; i ++) {
			Picture p   = DifficultDataUtils.getPicture(sId, mContext);
			float scale = ((float)drwa.getIntrinsicHeight() / p.getWidth());
			PictureDrawablePlus pic = new PictureDrawablePlus(p, p.getWidth() * scale);
//			imageView.setImageDrawable(pic);
		}
			
//		imageView.setVisibility(View.GONE);
		long pTime = System.currentTimeMillis() - biginTime; 
		countTime  = countTime + pTime;
		
		String about = "time:" + pTime + "ms/1000张";
		DifficultItem sItem = getDifficultItem("svg", 100, about);
		setProgress(sItem, (int)(((float)pTime / countTime) * 100));
		
		DifficultItem pItem = getDifficultItem("png", 100, sAbout);
		setProgress(pItem, (int)(((float)sTime / countTime) * 100));
		
		list.add(pItem);
		list.add(sItem);
		return list;
	}

	@SuppressLint("DefaultLocale")
	public DifficultEntity getDifficultImageSizeList() {
		DifficultEntity entity = new DifficultEntity();
		entity.title = DifficultDataUtils.res_size_name;
		entity.items = new CopyOnWriteArrayList<DifficultItem>();
		
		List<Integer> list = null;
		list = Arrays.asList(R.drawable.a16, R.drawable.a22, R.drawable.a44, R.drawable.a80, R.drawable.a128);
		Integer[] sizeArr = new Integer[list.size()];
		Resources res = mContext.getResources();
		
		long size = 0L;
		Bitmap bitmap = null;
		PictureDrawablePlus pDraw = null;
		InputStream in = null;
		
		for(int i = 0; i < list.size(); i ++) {
			bitmap = BitmapFactory.decodeResource(res, list.get(i));
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			bitmap.compress(CompressFormat.PNG, 100, os);
			int length = os.toByteArray().length;
			sizeArr[i] = length;
			
			StringBuffer sb = new StringBuffer();
			sb.append(getKBAbout(length));
			sb.append(getWHAbout(bitmap.getWidth(), bitmap.getHeight()));
			entity.items.add(getDifficultItem("png", 100, sb.toString()));
			
			size += length;
		}
		
		try {
			in = mContext.getResources().openRawResource(R.raw.icon01);
			Picture p = DifficultDataUtils.getPicture(R.raw.icon01, mContext);
			pDraw = new PictureDrawablePlus(p, 1.0f);
			size  = size + in.available();
			
			String about = getKBAbout((float)in.available());
			about += getWHAbout(pDraw.getIntrinsicWidth(), pDraw.getIntrinsicHeight());
			int progress = (int)((float)in.available() / size * 100);
			entity.items.add(getDifficultItem("svg", progress, about));
		} catch(IOException ex) {
			ex.printStackTrace();
		} finally {
			if(in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		for(int i = 0; i < list.size(); i ++) {
			DifficultItem item = entity.items.get(i);
			item.progress = (int)(((float)sizeArr[i] / size) * 100);
		}
		
		return entity;
	}
	
	public DifficultEntity getDifficultMemory() {
		DifficultEntity entity = new DifficultEntity();
		entity.title = DifficultDataUtils.use_memory_name;
		entity.items = new CopyOnWriteArrayList<DifficultItem>();
		
		long countMemory = 0;
		List<Integer> list = null;
		List<Integer> memorys = new CopyOnWriteArrayList<Integer>();
		list = Arrays.asList(R.drawable.a16, R.drawable.a22, R.drawable.a44, R.drawable.a80, R.drawable.a128);
		
		for(int i = 0; i < list.size(); i ++) {
			int memory = DifficultDataUtils.getBitmapMemory(list.get(i), mContext);
			countMemory += memory;
			memorys.add(memory);
			entity.items.add(getDifficultItem("png", 100, getKBAbout(memory)));
		}

		int svgMemory1 = DifficultDataUtils.getPictureDrwaMemory(R.raw.icon01, mContext, 1.0f);
		countMemory += svgMemory1;
			
		Drawable png_max_draw = null;
		png_max_draw = mContext.getResources().getDrawable(R.drawable.a128);
		int w = png_max_draw.getIntrinsicWidth();
		float scale = (float)(w / 16);
		Log.e("draw -> scale", scale + "");
//		int svgMemory2 = DifficultDataUtils.getPictureDrwaMemory(R.raw.icon01, mContext, scale);
//		countMemory += svgMemory2;
		
		int progress1 = (int)(((float)svgMemory1 / countMemory) * 100);
//		int progress2 = (int)(((float)svgMemory2 / countMemory) * 100);
		entity.items.add(getDifficultItem("svg", progress1, getKBAbout(svgMemory1)));
//		entity.items.add(getDifficultItem("svg", progress2, getKBAbout(svgMemory2)));
		
		for(int i = 0; i < list.size(); i++) {
			setProgress(entity.items.get(i), getIntProgress(memorys.get(i), countMemory));
		}
		
		return entity;
	}
	
	/** 
	 * 获取说明的width * height字符串
	 * @param w width
	 * @param h height
	 *  */
	private String getWHAbout(int w, int h) {
		return String.format("---%s*%s", w, h);
	}
	
	/** 
	 * 获取字节值的字符串
	 * @param byteVal 字节值
	 * */
	private String getKBAbout(float byteVal) {
		return String.format("%.2f", ((float)byteVal / 1024)) + "KB";
	}
	
	/** 
	 * 获取整型进度值0~99
	 * @param current 当前占用
	 * @param count 总占用
	 * */
	private int getIntProgress(int current, long count) {
		return (int)(((float)current / count) * 100);
	}
	
	private DifficultItem getDifficultItem(String n, int p, String a) {
		DifficultItem item = new DifficultItem();
		item.name = n;
		item.progress = p;
		item.about = a;
		return item;
	}
	
	private void setProgress(DifficultItem item, int progress) {
		item.progress = progress;
	}

}
